package cart.controller;

import cart.service.CartService;
import cart.vo.CartInfo;
import cart.vo.CartTemp;
import com.feign.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    CartService cartService;


    @PostMapping("addToCart/{skuId}/{skuNum}")
    public R addToCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum, HttpServletRequest request) {
        CartInfo cartItem = cartService.addToCart(skuId, skuNum, request);
        return R.ok().setData(cartItem);
    }

    @GetMapping("getCart")
    public R getCart(HttpServletRequest request) {
        CartTemp cart = cartService.getCart(request);
        return R.ok().setData(cart);
    }

    @DeleteMapping("deleteCart/{skuId}")
    public R deleteCart(@PathVariable Long skuId, HttpServletRequest request) {
        cartService.deleteCartInfo(skuId, request);
        return R.ok();
    }

    @GetMapping("checkCart/{skuId}/{isChecked}")
    public R isCheck(@PathVariable Long skuId, @PathVariable Integer isChecked, HttpServletRequest request) {
        cartService.checked(skuId, isChecked, request);
        return R.ok();
    }

    @GetMapping("checkedCart")
    public List<CartInfo> getCheckedCart(@RequestHeader(value = "token") String token) {
        return cartService.getCheckedCart(token);
    }

}
